<template>
  <div class="user-management-container">
    <!-- 顶部标题和新建按钮 -->
    <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
      <span style="line-height:60px;font-size:20px;">信息管理</span>
      <div style="float:right;">
        <el-button type="primary" @click="beforeAddUser">新建用户</el-button>
      </div>
    </div>

    <!-- 搜索区域 -->
    <el-card class="search-card">
      <el-form :inline="true">
        <el-form-item label="用户姓名">
          <el-input placeholder="请输入用户姓名" v-model="searchForm.nickName"></el-input>
        </el-form-item>
        <el-form-item label="用户手机号">
          <el-input placeholder="请输入手机号" v-model="searchForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="渠道来源">
          <el-select placeholder="请选择"  >
            <el-option label="支付宝" value="10"></el-option>
            <el-option label="微信" value="20"></el-option>
            <el-option label="小红书" value="30"></el-option>
            <el-option label="拼夕夕" value="40"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="注册时间">
          <el-date-picker
              type="datetimerange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="YYYY-MM-DD HH:mm:ss"
              format="YYYY-MM-DD HH:mm:ss"
              v-model="times"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button @click="resetSearch">重置</el-button>
          <el-button type="primary" @click="loadUserList">搜索</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 用户表格 -->
    <el-card class="table-card">
      <el-table :data="userList" border stripe>
        <el-table-column prop="id" label="编号" width="80" align="center"></el-table-column>
        <el-table-column prop="userName" label="用户名" width="100" align="center"></el-table-column>
        <el-table-column prop="password" label="密码" width="80" align="center"></el-table-column>
        <el-table-column prop="phone" label="手机号" width="300" align="center"></el-table-column>
        <el-table-column prop="ipaddress" label="IP地址" width="120" align="center"></el-table-column>
        <el-table-column prop="status" label="状态" width="120" align="center"></el-table-column>
        <el-table-column prop="channel" label="注册渠道" width="100" align="center"></el-table-column>
        <el-table-column prop="regtime" label="注册时间" width="350" align="center"></el-table-column>
        <el-table-column label="操作" width="150" align="center">
          <template #default="scope">
            <el-button type="text" @click="showDetail(scope.row.id)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 用户详情/编辑对话框 -->
    <el-dialog
        :title="dialogTitle"
        v-model="dialogVisible"
        width="50%"
        :before-close="handleClose"
    >
      <el-form :model="userForm" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="手机号">
              <el-input v-model="userForm.phone" placeholder="请输入手机号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="账号">
              <el-input   placeholder="请输入账号" :disabled="isEdit"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="用户名">
              <el-input v-model="userForm.userName" placeholder="请输入用户名"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="密码">
              <el-input v-model="userForm.password" placeholder="请输入密码" ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="昵称">
              <el-input v-model="userForm.nickName" placeholder="请输入昵称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="IP地址">
              <el-input v-model="userForm.ipAddress" placeholder="请输入IP地址" ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="注册渠道">
          <el-select v-model="userForm.providerType" placeholder="请选择注册渠道">
            <el-option label="0" value="网站"></el-option>
            <el-option label="APP" value="APP"></el-option>
            <el-option label="小程序" value="小程序"></el-option>
            <el-option label="后台添加" value="后台添加"></el-option>
            <el-option label="未知" value="未知"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="handleClose">取消</el-button>
        <el-button v-if="isEditMode"  type="primary" @click="saveUser">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import {ref, reactive, nextTick, onMounted} from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import axios from "axios";
// 用户列表数据
const userList = ref([])

// 添加一个响应式变量控制模式
const isEditMode = ref(false)

// 搜索表单
const searchForm = ref({
  nickName: '',
  phone: ''
})

// 用户表单
const userForm = ref({
  id:'',
  nickName: '',
  ipAddress:'',
  userName: '',
  password: '',
  phone: '',
  regTime: ''
})

// 对话框控制
const dialogVisible = ref(false)
const dialogTitle = ref('新增用户')
const isEdit = ref(false)


// 加载用户列表
const loadUserList = () => {
  console.log('加载用户列表', searchForm.value)
  axios.post('http://localhost:7071/basic/user/list',searchForm.value).then((response)=>{
    console.log(response);
    if(response.data.code==='0'){
      userList.value = response.data.data;
    }else{
      ElMessage.error('加载用户列表失败')
    }
  })
}

// 重置搜索
const resetSearch = () => {
  searchForm.value.nickName = ''
  searchForm.value.phone = ''
  ElMessage.success('重置成功')
}

// 准备添加用户
const beforeAddUser = () => {
  dialogVisible.value = true
  dialogTitle.value = '新建用户'
  isEdit.value = true
  isEditMode.value = true
  Object.assign(userForm, {
    id: '',
    title: '',
    gender: '',
    phone: '',
    account: '',
    tags: [],
    channel: ''
  })
  dialogVisible.value = true
}

// 查看用户详情
const showDetail = (id) => {
  isEditMode.value = false
  console.log('查看用户详情', id)
  dialogTitle.value = '用户详情'
  axios.post('http://localhost:7071/basic/user/detail/'+id).then((response)=>{
    console.log(response.data.data);
    if(response.status===200){
      userForm.value.id = response.data.data.id;
      userForm.value.userName= response.data.data.userName;
      userForm.value.title = response.data.data.title;
      userForm.value.phone = response.data.data.phone;
      userForm.value.account = response.data.data.account;
      userForm.value.nickName = response.data.data.nickName;
      userForm.value.regTime = response.data.data.regTime;
      userForm.value.password = response.data.data.password;
      userForm.value.ipAddress = response.data.data.ipAddress;
      console.log(userForm.value);
    }else{
      ElMessage.error('用户不存在');
    }
  })
  dialogVisible.value = true
}

onMounted(() => {
  loadUserList()
})

// 关闭对话框
const handleClose = () => {
  dialogVisible.value = false
}

// 保存用户
const saveUser = () => {
  console.log('保存用户', userForm.value)
  axios.post('http://localhost:7071/basic/user/info/register',userForm.value).then((response)=>{
    if(response.status===200){
      ElMessage.success('保存成功');
      dialogVisible.value = false;
      loadUserList();
    }else{
      ElMessage.error('保存失败');
    }
  })
}

</script>

<style scoped>
.user-management-container {
  padding: 20px;
  background-color: #f5f7fa;
}

.header {
  height: 60px;
  padding: 10px 20px;
  background-color: #fff;
  margin-bottom: 10px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.title {
  font-size: 20px;
  line-height: 60px;
}

.add-button {
  margin-top: 10px;
}

.search-card {
  margin-bottom: 10px;
}

.table-card {
  margin-bottom: 20px;
}

.el-tag {
  margin-right: 10px;
}
</style>